import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';

import { getFilterOptions } from '/@/api/sys/menu';
import { getDictItems } from '/@/api/common/api';
import { ApiSelect } from '/@/components/Form';
import { h, ref } from 'vue';
import result from '../../../types/module';

export const columns: BasicColumn[] = [
    {
        title: 'guid',
        resizable:true,
        dataIndex: 'guid'
    },
    {
        title: 'tag',
        resizable:true,
        dataIndex: 'tag'
    },
    {
        title: 'path',
        resizable:true,
        dataIndex: 'path'
    },
    {
        title: '信号名称',
        resizable:true,
        dataIndex: 'name'
    },
    {
        title: '（1 空间节点   2设备节点   3 点位节点）',
        resizable:true,
        dataIndex: 'nodeType'
    },
    {
        title: '删除状态',
        resizable:true,
        dataIndex: 'delFlag'
    },
    {
        title: '点位分类Point type：1=AI；2=DI；3=AO；4=DO；5=Alarm。',
        resizable:true,
        dataIndex: 'pointType'
    },
    {
        title: '点位的单位',
        resizable:true,
        dataIndex: 'unit'
    },
    {
        title: 'DI/DO的取值含义。格式如：“1=开门;0=关门”。 DI/DO的取值含义。格式如：“1=开门;0=关 必须是数值型的值。',
        resizable:true,
        dataIndex: 'statusMap'
    },
    {
        title: '告警级别',
        resizable:true,
        dataIndex: 'alarmLevel'
    },
    {
        title: '告警类型',
        resizable:true,
        dataIndex: 'alarmType'
    },
    {
        title: '上传数据的时间间隔，单位秒',
        resizable:true,
        dataIndex: 'period'
    },
    {
        title: '变比',
        resizable:true,
        dataIndex: 'percentage'
    },
    {
        title: '绝对值变比',
        resizable:true,
        dataIndex: 'absValue'
    },
    {
        title: 'AO的合法控制范围。max=30。默认NULL，AO的合法控制范围。不生效。',
        resizable:true,
        dataIndex: 'aoMaxBound'
    },
    {
        title: '围 ',
        resizable:true,
        dataIndex: 'aoMinBound'
    },
    {
        title: 'parentGuid',
        resizable:true,
        dataIndex: 'parentGuid'
    },
    {
        title: '映射监测类型',
        resizable:true,
        dataIndex: 'mpPorintType'
    },
    {
        title: '信号类型',
        resizable:true,
        dataIndex: 'mpSingType'
    },
    {
        title: '标准监控信息名称',
        resizable:true,
        dataIndex: 'mpPointName'
    },
    {
        title: '映射告警信息',
        resizable:true,
        dataIndex: 'mpAlarm'
    },
    {
        title: '编码',
        resizable:true,
        dataIndex: 'mpCode'
    },
];

export const searchFormSchema: FormSchema[] = [
    {
        label: 'guid',
        field: 'guid',
        component: 'Input'
    },
    {
        label: 'tag',
        field: 'tag',
        component: 'Input'
    },
];
let apiSelectValue = [];
export const formSchema: FormSchema[] = [
    { label: '', field: 'id', component: 'Input', show: false },
    { label: '', field: 'ids', component: 'Input', show: false },
    { label: '', field: 'optionType', component: 'Input', show: false },
    // TODO 主键隐藏字段，目前写死为ID
    {
        label: '名称',
        field: 'name',
        component: 'Input',
        componentProps: {
            disabled: true
        },
    },
    {
        label: '设备大类',
        field: 'mpBigType',
        component: 'ApiSelect',
        render: ({ model, field }) => {
            //渲染自定义组件，以Input为例
            return h(ApiSelect, {
                api: () => {
                    return getFilterOptions(1) as any
                },
                allowClear: true,
                resultField: 'result',
                labelField: 'mpBigType',
                valueField: 'mpBigType',
                showSearch: true,
                value: model.mpBigType,
                onChange: (e: ChangeEvent) => {
                    if (e == model.mpBigType) {
                        return
                    }
                    model.mpBigType = e
                    model.mpDeviceType = ''
                    model.mpPointName = ''
                    model.mpAlarm = ''
                    model.mpCode = ''
                },
            });
        },
    },
    {
        label: '设备类型',
        field: 'mpDeviceType',
        component: 'ApiSelect',
        render: ({ model, field }) => {
            //渲染自定义组件，以Input为例
            return h(ApiSelect, {
                api: () => {
                    return getFilterOptions(2, (model.mpBigType instanceof Array) ? model.mpBigType.join("") : model.mpBigType) as any
                },
                allowClear: true,
                resultField: 'result',
                labelField: 'mpDeviceType',
                valueField: 'mpDeviceType',
                showSearch: true,
                value: model.mpDeviceType,
                onChange: (e: ChangeEvent) => {
                    if (e == model.mpDeviceType) {
                        return
                    }
                    model.mpDeviceType = e
                    model.mpPointName = ''
                    model.mpAlarm = ''
                    model.mpCode = ''
                    // model[field] = e.target.value;
                },
            });
        },
    },
    {
        label: '映射监测类型',
        field: 'mpPorintType',
        component: 'ApiSelect',
        componentProps: {
            api: () => getDictItems('mpPorintType'),
            resultField: 'result',
            labelField: 'text',
            showSearch: true,
            valueField: 'value',
        },
    },
    {
        label: '名称/编码',
        field: 'codeAndName',
        component: 'ApiSelect',
        render: ({ model, field }) => {
            //渲染自定义组件，以Input为例
            return h(ApiSelect, {

                api: () => {
                    return getFilterOptions(3,
                        (model.mpBigType instanceof Array) ? model.mpBigType.join("") : model.mpBigType,
                        (model.mpPorintType instanceof Array) ? model.mpPorintType.join("") : model.mpPorintType,
                        (model.mpDeviceType instanceof Array) ? model.mpDeviceType.join("") : model.mpDeviceType,
                        (model.codeAndName instanceof Array) ? model.codeAndName.join("") : model.codeAndName
                    ).then((res: any) => {
                        apiSelectValue = res.result;
                        return res;
                    }) as any
                },
                onSearch:(e)=>{
                    model.codeAndName = e
                },
                resultField: 'result',
                allowClear: true,
                labelField: 'codeAndName',
                valueField: 'codeAndName',
                showSearch: true,
                value: model.codeAndName,
                onChange: (e: any) => {
                    if (e == model.mpPointName) {
                        return
                    }
                    getFilterOptions(3,
                        (model.mpBigType instanceof Array) ? model.mpBigType.join("") : model.mpBigType,
                        (model.mpPorintType instanceof Array) ? model.mpPorintType.join("") : model.mpPorintType,
                        (model.mpDeviceType instanceof Array) ? model.mpDeviceType.join("") : model.mpDeviceType
                    ).then((res: any) => {
                        if (e) {
                            if (typeof e == "string") {
                                model.codeAndName = e
                                model.mpCode = e.split("-")[0]
                                model.mpPointName = e.split("-")[1]
                                const findIndex = res.findIndex((item: any) => {
                                    return item.codeAndName == e
                                });
                                model.mpAlarm = res[findIndex].mpAlarm
                            }

                        } else {
                            model.codeAndName = ""
                            model.mpCode = ""
                            model.mpPointName = ""
                            model.mpAlarm = ""
                        }

                    })


                    // model[field] = e.target.value;
                },
            });
        },
    },
    {
        label: '标准监控信息名称',
        field: 'mpPointName',
        component: 'ApiSelect',
        render: ({ model, field }) => {
            //渲染自定义组件，以Input为例
            return h(ApiSelect, {
                api: () => {
                    return getFilterOptions(3,
                        (model.mpBigType instanceof Array) ? model.mpBigType.join("") : model.mpBigType,
                        (model.mpPorintType instanceof Array) ? model.mpPorintType.join("") : model.mpPorintType,
                        (model.mpDeviceType instanceof Array) ? model.mpDeviceType.join("") : model.mpDeviceType
                    ) as any
                },
                resultField: 'result',
                allowClear: true,
                labelField: 'mpPointName',
                valueField: 'mpPointName',
                showSearch: true,
                value: model.mpPointName,
                onChange: (e: ChangeEvent) => {
                    if (e == model.mpPointName) {
                        return
                    }
                    model.mpPointName = e
                    model.mpAlarm = ''
                    model.mpCode = ''
                    // model[field] = e.target.value;
                },
            });
        },

    },
    {
        label: '编码',
        field: 'mpCode',
        component: 'ApiSelect',
        render: ({ model, field }) => {
            //渲染自定义组件，以Input为例
            return h(ApiSelect, {
                api: () => {
                    return getFilterOptions(3,
                        (model.mpBigType instanceof Array) ? model.mpBigType.join("") : model.mpBigType,
                        (model.mpPorintType instanceof Array) ? model.mpPorintType.join("") : model.mpPorintType,
                        (model.mpDeviceType instanceof Array) ? model.mpDeviceType.join("") : model.mpDeviceType
                    ) as any
                },
                resultField: 'result',
                labelField: 'mpName',
                allowClear: true,
                valueField: 'mpCode',
                showSearch: true,
                value: model.mpCode,
                onChange: (e: ChangeEvent) => {
                    model.mpCode = e
                    // model[field] = e.target.value;
                },
            });
        },
    },
    {
        label: '告警信息',
        field: 'mpAlarm',
        component: 'JSelectInput',
        render: ({ model, field }) => {
            //渲染自定义组件，以Input为例
            return h(ApiSelect, {
                api: () => {
                    return getFilterOptions(3,
                        (model.mpBigType instanceof Array) ? model.mpBigType.join("") : model.mpBigType,
                        (model.mpPorintType instanceof Array) ? model.mpPorintType.join("") : model.mpPorintType,
                        (model.mpDeviceType instanceof Array) ? model.mpDeviceType.join("") : model.mpDeviceType
                    ) as any
                },
                resultField: 'result',
                labelField: 'mpAlarm',
                allowClear: true,
                valueField: 'mpAlarm',
                showSearch: true,
                value: model.mpAlarm,
                onChange: (e: ChangeEvent) => {
                    console.log(123)
                    console.log(e)
                    if (e == model.mpAlarm) {
                        return
                    }
                    model.mpAlarm = e
                    model.mpCode = ''
                    // model[field] = e.target.value;
                },
            });
        },

    },
    {
        label: '序号',
        field: 'secno',
        component: 'Input',
        componentProps: {
            disabled: false,
        },
    },
    // {
    //     label: '数据类型',
    //     field: 'type',
    //     component: 'Input',
    // },
    // {
    //     label: '监控数据标识号',
    //     field: 'cnId',
    //     component: 'Input',
    // },
    // {
    //     label: '信号名称',
    //     field: 'signalname',
    //     component: 'Input',
    // },
];
